package com.my.learn.nio.netty.longConnection;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;

import java.util.List;

/**
 * @author fu
 * @version 1.0
 * @description TODO
 * @date 2021-11-05 15:11
 */
public class ProtostuffDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        Schema<SendModel> sendModelSchema = RuntimeSchema.getSchema(SendModel.class);
        SendModel sendModel = new SendModel();
        byte[] array = new byte[in.readableBytes()];
        in.readBytes(array);
        ProtostuffIOUtil.mergeFrom(array, sendModel, sendModelSchema);
        out.add(sendModel);
    }

}
